package com.thti.mecool.client.util;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.StrikethroughSpan;
import android.util.Log;
import android.widget.Toast;

/**
 * 
 * @author weiguo.ren
 * 
 */
public class Util {

	// 日志Tag
	private static String TAG = "Mecool";
	// 日志打印开关
	private static boolean print = true;

	public static void print(Object msg) {
		if (print) {
			Log.i(TAG, msg.toString());
		}
	}

	/**
	 * 发送Toast
	 * 
	 * @param mContext
	 * @param text
	 */
	public static void sendToast(Context mContext, String text) {
		Toast.makeText(mContext, text, 0).show();
	}

	/**
	 * 删除线
	 * 
	 * @param text
	 * @return
	 */
	public static SpannableString strickout(String text) {
		SpannableString ss = new SpannableString(text);
		ss.setSpan(new StrikethroughSpan(), 0, ss.length(),
				Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
		return ss;
	}

	/**
	 * 拨打电话
	 * 
	 * @param mActivity
	 * @param telNumber
	 *            电话号码
	 */
	public static void call(final Activity mActivity) {
		if (StringUtil.isNotBlank(ConfigUtil.customerPhone)) {
			AlertDialog.Builder builder = new Builder(mActivity);
			builder.setMessage("是否拨打客服电话");
			builder.setTitle("提示");
			builder.setPositiveButton("确定",
					new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							dialog.dismiss();
							try {
								Intent intent = new Intent(Intent.ACTION_CALL,
										Uri.parse("tel:"
												+ ConfigUtil.customerPhone));
								mActivity.startActivity(intent);
							} catch (Exception e) {
								Util.sendToast(mActivity, "呼叫失败");
								e.printStackTrace();
							}
						}
					});
			builder.setNegativeButton("取消",
					new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							dialog.dismiss();

						}
					});
			builder.create().show();
		}else{
			Util.sendToast(mActivity, "客服号码为空");
		}
	}
	
	
	/**
	 * 读取本机电话本
	 * @param activity 当前调用改方法的Activity
	 * @return
	 */
	public static String readerPhone(Activity activity){
		 Cursor cur = activity.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
		 activity.startManagingCursor(cur);
			StringBuffer sb = new StringBuffer();
			int i=0;
	        while (cur.moveToNext()) {
	 
				long id = cur.getLong(cur.getColumnIndex("_id"));
				Cursor pcur = activity.getContentResolver().query(
						ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
						null,
						ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "="
								+ Long.toString(id), null, null);
				String phoneNumbers = "";
				while (pcur.moveToNext()) {
					String strPhoneNumber = pcur
							.getString(pcur
									.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
					phoneNumbers += strPhoneNumber + "|";
				}
				pcur.close();
	 
				String name = cur.getString(cur.getColumnIndex("display_name"));
				if(phoneNumbers.length()>1){
					String flag =phoneNumbers.substring(phoneNumbers.length()-1, phoneNumbers.length());
					if(flag.length()>0&&flag.equals("|")){
						phoneNumbers=phoneNumbers.substring(0, phoneNumbers.length()-1);
					}
				}
//				listData.add(phoneNumbers);
				sb.append(i==0?"":",");
				sb.append("{'userName':'"+name+"','phoneNumber':'"+phoneNumbers+"'}");
				i++;
//				sb.append(name+":"+phoneNumbers+"\n");
			}
			cur.close();
		    return sb.toString();
	}

}
